package com.atguigu.eduService.controller;


import com.atguigu.common.Result;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.vo.CourseInfoVo;
import com.atguigu.eduService.entity.vo.CoursePublishVo;
import com.atguigu.eduService.entity.vo.CourseQuery;
import com.atguigu.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-07-27
 */
@RestController
@RequestMapping("/edu/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;


    @ApiOperation(value = "获取课程列表")
    @GetMapping("/getAll")
    public Result getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return Result.ok().data("list",list);
    }

    @ApiOperation(value = "分页条件查询课程列表")
    @PostMapping("/getPageList")
    public Result getCoursePageListExample(@RequestParam("current")Integer current,
                                    @RequestParam("size")Integer size,
                                    @RequestBody(required = false) CourseQuery courseQuery){

        IPage<EduCourse> page = courseService.pageQuery(current,size,courseQuery);
        return Result.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/getPage")
    public Result getPage(@RequestParam("current")Integer current,
                          @RequestParam("size")Integer size){

        Page<EduCourse> page = new Page<>(current,size);
        courseService.page(page,null);
        return Result.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }


    @ApiOperation(value = "添加课程")
    @PostMapping
    public Result add(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加课程之后的id,以供后面使用
        String courseId = courseService.addCourse(courseInfoVo);
        return Result.ok().data("courseId",courseId);

    }
    @ApiOperation(value = "获取课程信息")
    @GetMapping("/{courseId}")
    public Result get(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo = courseService.getInfo(courseId);
        return Result.ok().data("course",courseInfoVo);
    }
    @ApiOperation(value = "更新课程信息")
    @PutMapping
    public Result update(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateInfo(courseInfoVo);
        return Result.ok();
    }
    @ApiOperation(value = "获取发布课程信息对象")
    @GetMapping("/getPublish/{courseId}")
    public Result getPublishCourse(@PathVariable("courseId") String courseId){
        CoursePublishVo publishInfo = courseService.getPublishCourse(courseId);
        return Result.ok().data("publishInfo",publishInfo);
    }

    @ApiOperation(value = "课程发布")
    @PostMapping("/finalPublish/{courseId}")
    public Result finalPublish(@PathVariable("courseId") String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        return courseService.updateById(eduCourse)?Result.ok():Result.error();

    }

    @ApiOperation(value = "根据课程id删除课程")
    @DeleteMapping("/{courseId}")
    public Result deleteCourse(@PathVariable("courseId") String courseId){
        courseService.deleteCourse(courseId);
        return Result.ok();
    }


}

